import router from '../router'
import store from '../store'

router.beforeEach((to, from, next) => {
    

    const token = store.state.token || "";
    const loginUrl = store.state.loginUrl || '/loginUrl';
    const permissionList = store.state.permissionList || [];

    if (to.meta.isNotNeedLogin) {
        next();
        return;
    }

    // 存在token，
    if (token) {
        // 处理资源，
        if (permissionList.length < 0) {
            // 从后台获取资源信息
            store.commit("updatePermissionMenu", [{},{}]);
            next({
                ...to,
                replace: true
            })
        }
        // 这里判断路由来源
        if (Object.keys(from.query).length === 0) { //判断路由来源是否有query，处理不是目的跳转的情况
            next()
        } else {
            const redirect = from.query.redirect
            if (to.path === redirect) {
                next()
            } else {
                next({
                    path: redirect
                })
            }
        }
    } else {
        if (to.path === loginUrl) {
            next()
        } else {
            next({
                path: loginUrl,
                query: {
                    redirect: to.fullPath
                }
            })
        }
    }
});

router.afterEach((to, from) => {
    
})